﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Deskopeia.Helper
{
#warning check whether this class is up-to-date (compare to wintools)
#warning remove unused stuff

    public static class DesktopWindowHelper
    {
        public enum DesktopWindow
        {
            ProgMan,
            SHELLDLL_DefViewParent,
            SHELLDLL_DefView,
            SysListView32
        }

        public static IntPtr GetDesktopWindow(DesktopWindow desktopWindow)
        {
            IntPtr ProgMan = Win32APIImports.GetShellWindow();
            IntPtr SHELLDLL_DefViewParent = ProgMan;
            IntPtr SHELLDLL_DefView = Win32APIImports.FindWindowEx(ProgMan, IntPtr.Zero, "SHELLDLL_DefView", null);
            IntPtr SysListView32 = Win32APIImports.FindWindowEx(SHELLDLL_DefView, IntPtr.Zero, "SysListView32", "FolderView");

            if (SHELLDLL_DefView == IntPtr.Zero)
            {
                Win32APIImports.EnumWindows((hwnd, lParam) =>
                {
                    if (Win32APIImports.GetClassName(hwnd) == "WorkerW")
                    {
                        IntPtr child = Win32APIImports.FindWindowEx(hwnd, IntPtr.Zero, "SHELLDLL_DefView", null);
                        if (child != IntPtr.Zero)
                        {
                            SHELLDLL_DefViewParent = hwnd;
                            SHELLDLL_DefView = child;
                            SysListView32 = Win32APIImports.FindWindowEx(child, IntPtr.Zero, "SysListView32", "FolderView"); ;
                            return false;
                        }
                    }
                    return true;
                }, IntPtr.Zero);
            }

            switch (desktopWindow)
            {
                case DesktopWindow.ProgMan:
                    return ProgMan;
                case DesktopWindow.SHELLDLL_DefViewParent:
                    return SHELLDLL_DefViewParent;
                case DesktopWindow.SHELLDLL_DefView:
                    return SHELLDLL_DefView;
                case DesktopWindow.SysListView32:
                    return SysListView32;
                default:
                    return IntPtr.Zero;
            }
        }
    }
}
